<template>
  <router-link :to="`/video/${video.id}`" class="video-card">
    <div class="video-card-content">
      <!-- 视频封面 -->
      <div class="video-cover">
        <img 
          :src="video.cover_url || '/default-cover.svg'" 
          :alt="video.title" 
          class="cover-img"
          @error="handleImageError"
        />
        <!-- 视频时长 -->
        <span class="video-duration">{{ video.duration }}</span>
      </div>
      
      <!-- 视频信息 -->
      <div class="video-info">
        <h3 class="video-title">{{ video.title }}</h3>
        <div class="video-stats">
          <span class="view-count">{{ formatCount(video.view_count) }}</span>
          <span class="upload-time">{{ formatTime(video.published_at) }}</span>
        </div>
        <div class="video-uploader">
          <img 
            :src="video.avatar || '/default-avatar.svg'" 
            :alt="video.nickname || video.username" 
            class="uploader-avatar"
            @error="handleAvatarError"
          />
          <span class="uploader-name">{{ video.nickname || video.username }}</span>
        </div>
      </div>
    </div>
  </router-link>
</template>

<script>
export default {
  name: 'VideoCard',
  props: {
    video: {
      type: Object,
      required: true
    }
  },
  setup(props) {
    // 格式化数量（如1000 -> 1万）
    const formatCount = (count) => {
      if (count === undefined || count === null) {
        return '0';
      }
      if (count >= 10000) {
        return (count / 10000).toFixed(1) + '万';
      }
      return count.toString();
    };

    // 格式化时间（显示相对时间或日期）
    const formatTime = (dateString) => {
      if (!dateString) return '';
      
      const date = new Date(dateString);
      const now = new Date();
      const diff = now - date;
      
      // 计算相差天数
      const days = Math.floor(diff / (1000 * 60 * 60 * 24));
      
      if (days === 0) {
        // 今天发布
        const hours = Math.floor(diff / (1000 * 60 * 60));
        if (hours === 0) {
          const minutes = Math.floor(diff / (1000 * 60));
          return minutes < 1 ? '刚刚' : `${minutes}分钟前`;
        }
        return `${hours}小时前`;
      } else if (days === 1) {
        return '1天前';
      } else if (days < 7) {
        return `${days}天前`;
      } else if (days < 30) {
        const weeks = Math.floor(days / 7);
        return `${weeks}周前`;
      } else if (days < 365) {
        const months = Math.floor(days / 30);
        return `${months}个月前`;
      } else {
        const years = Math.floor(days / 365);
        return `${years}年前`;
      }
    };

    // 处理封面图片加载失败
    const handleImageError = (event) => {
      event.target.src = '/default-cover.svg';
    };

    // 处理头像加载失败
    const handleAvatarError = (event) => {
      event.target.src = '/default-avatar.svg';
    };

    return {
      formatCount,
      formatTime,
      handleImageError,
      handleAvatarError
    };
  }
};
</script>

<style scoped>
.video-card {
  display: block;
  text-decoration: none;
  color: #333;
  transition: transform 0.2s;
}

.video-card:hover {
  transform: translateY(-4px);
}

.video-card-content {
  display: flex;
  flex-direction: column;
}

/* 视频封面样式 */
.video-cover {
  position: relative;
  width: 100%;
  padding-top: 56.25%; /* 16:9 比例 */
  margin-bottom: 8px;
  overflow: hidden;
  border-radius: 4px;
  background-color: #f0f0f0;
}

.cover-img {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s;
}

.video-card:hover .cover-img {
  transform: scale(1.05);
}

.video-duration {
  position: absolute;
  bottom: 4px;
  right: 4px;
  background-color: rgba(0, 0, 0, 0.7);
  color: white;
  padding: 2px 6px;
  font-size: 12px;
  border-radius: 2px;
}

/* 视频信息样式 */
.video-info {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.video-title {
  font-size: 14px;
  font-weight: 500;
  line-height: 1.4;
  margin: 0;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
}

.video-stats {
  display: flex;
  gap: 8px;
  font-size: 12px;
  color: #999;
}

.video-uploader {
  display: flex;
  align-items: center;
  gap: 6px;
}

.uploader-avatar {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background-color: #f0f0f0;
}

.uploader-name {
  font-size: 12px;
  color: #999;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
}

/* 响应式调整 */
@media (max-width: 768px) {
  .video-title {
    font-size: 13px;
  }
  
  .video-stats,
  .uploader-name {
    font-size: 11px;
  }
}
</style>